package com.ld.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.mapper.ClassesMapper;
import com.ld.mapper.StudentMapper;
import com.ld.pojo.Student;
import com.ld.service.StudentService;
import com.ld.util.PageResult;
import com.ld.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

	@Autowired
	private ClassesMapper classesMapper;

	//1. 查询所有学生列表
	@Override
	public List<Student> findAll() {
		return getBaseMapper().selectList(null);
	}

	//2. 添加学生
	@Override
	public void add(Student student) {

		baseMapper.insert(student);
	}

	//3. 分页查询学生列表
	@Override
	public PageResult<Student> findByPage(int page, int pagesize) {
		//3.1 定义分页参数对象
		IPage<Student> pageStud = new Page<>(page,pagesize);
		//3.2 定义查询对象
		Wrapper<Student> queryWrapper = new QueryWrapper<>();
		//3.3 开始分页查询
		IPage<Student> studentIPage = baseMapper.selectPage(pageStud, queryWrapper);
		//3.4 遍历每个学生，并为学生指定班级名称
		for (Student student : studentIPage.getRecords()) {
			String cname = classesMapper.selectById(student.getCid()).getCname();
			student.setCname(cname);
		}
		//3.5 返回
		return new PageResult<>(studentIPage.getTotal(),studentIPage.getRecords());
	}

	//4. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
		//4.1 定义分页参数对象
		IPage<Student> pageStud = new Page<>(page,pagesize);
		//4.2 定义查询对象
		//4.2.1 方法一：（传统写法）
		// QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		// if(vo != null){
		// 	if(!StringUtils.isEmpty(vo.getSname())){
		// 		queryWrapper.like("sname",vo.getSname());
		// 	}
		// 	if(!StringUtils.isEmpty(vo.getAddr())){
		// 		queryWrapper.like("addr",vo.getAddr());
		// 	}
		// 	if(vo.getCid() != null && vo.getCid() != 0){
		// 		queryWrapper.eq("cid",vo.getCid());
		// 	}
		// }
		//非链式语法分页写法
		// baseMapper.selectPage(studentIPage,queryWrapper);

		//4.2.2 方法二：（链式写法）:
		QueryChainWrapper<Student> queryWrapper = new QueryChainWrapper<>(baseMapper);
		queryWrapper.like("sname",vo.getSname())
				.like("addr",vo.getAddr());
		if(vo.getCid() != null && vo.getCid() != 0) {
			queryWrapper.eq("cid", vo.getCid());
		}

		//4.2.3 方法三：（lamba表达式写法）
		// LambdaQueryChainWrapper<Student> queryWrapper = new LambdaQueryChainWrapper<>(baseMapper);
		// chainWrapper.like(Student::getSname,vo.getSname())
		// 		       .like(Student::getAddr,vo.getAddr());
		// if(vo.getCid() != null && vo.getCid() != 0){
		// 	chainWrapper.eq(Student::getCid,vo.getCid());
		// }
		//4.3 开始分页查询(链式语法的分页写法)
		IPage<Student> studentIPage = queryWrapper.page(pageStud);

		//4.4 遍历每个学生，并为学生指定班级名称
		for (Student student : studentIPage.getRecords()) {
			String cname = classesMapper.selectById(student.getCid()).getCname();
			student.setCname(cname);
		}
		//4.5 返回
		return new PageResult<>(studentIPage.getTotal(),studentIPage.getRecords());
	}
}
